package com.keshe.service;

import com.keshe.dao.UserDao;
import com.keshe.domain.User;

import java.util.List;

public class UserContorller {
    //注册功能
    public int login(String id, String password) {
        if (id.equals("") || password.equals("")) {
            return 0;
        }
        //查验账号是否存在
        UserDao us = new UserDao();
        List<User> list = us.selectId(id);//查询数据库中是否有该账号
        System.out.println("用户名：" + id + "\n密码：" + password);
        if (list.size() == 0) {
            //账号不存在
            User user = new User(id, password);
            int i = us.insertUSer(user);
            if (i == 1) {
                System.out.println("注册成功");
                return 1;
            } else {
                System.out.println("注册失败");
                return -1;
            }
        } else {
            System.out.println("账号已存在");
            return -2;
        }
    }

    //登录
    public int login_in(String id, String password) {
        UserDao us = new UserDao();
        List<User> list = us.selectId(id);//查询数据库中是否有该账号
        System.out.println("用户名：" + id + "\n密码：" + password);
        if (id.equals("") || password.equals("")) {
            return 0;
        }
        if (list.size() == 0) {
            System.out.println("当前无该账号，请先注册");
        } else {
            User user = list.get(0);
            if (user.getPassword().equals(password)) {
                System.out.println("登录成功");
                return 1;
            } else {
                System.out.println("登录失败");
                return -1;
            }
        }
        return -1;
    }

    //修改密码
    public int updatePassword(String id,String olePassword,String newPassword){
        UserDao us=new UserDao();
        int count=0;
        List<User> user= us.selectId(id);
        if(id.equals("")||olePassword.equals("")||newPassword.equals("")){
            return -2;
        }
        if(user.size()==0){
            return -3;
        }
        if(user.get(0).getPassword().equals(olePassword)){
            count=us.updateUser(id,newPassword);
            return count;
        }else{
            System.out.println("原密码错误");
            return -1;
        }
    }
}
